//
//  ViewController.m
//  AZNetwork
//
//  Created by GorXion on 2018/4/2.
//  Copyright © 2018年 gaoX. All rights reserved.
//

#import "ViewController.h"

#import "FKAssemblyModel.h"
#import "FKEventModel.h"
#import "AZBaseModel+Map.h"
#import "AZURLRequest+Configure.h"
#import "FKResponse.h"
#import "FKEventTarget.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AZURLRequest *request = [AZURLRequest requestWithBaseURL:[NSURL URLWithString:@"https://api.github.com/"] path:@"search/repositories" parameters:@{@"access_token": @"7b9ae377d3894bb20dbeaa495e7162f83a275112", @"q": @"swift"}];
    request.requestSerializer.timeoutInterval = 10;
    [request startWithSuccess:^(id response) {
        FKAssemblyListModel *model = [FKAssemblyListModel mapObject:response];
        [model.items enumerateObjectsUsingBlock:^(FKAssemblyModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"%@", obj.name);
        }];
    } failure:^(NSError *error) {
        NSLog(@"failure");
    }];
    
    AZURLRequest *uploadReq = [AZURLRequest requestWithBaseURL:[NSURL URLWithString:@"http://139.196.176.51:82/"] path:@"m/customer/avatar" parameters:nil];
    uploadReq.method = AZHTTPMethodUpload;
    uploadReq.requestSerializer.timeoutInterval = 10;
    uploadReq.constructingBodyWithBlock = ^(id<AFMultipartFormData> formData) {
        NSData *imgData = UIImageJPEGRepresentation([UIImage imageNamed:@"icon"], 0.9);
        NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
        NSString *imgName = [NSString stringWithFormat:@"%.f.jpg", interval];
        if (imgData && imgName) {
            [formData appendPartWithFileData:imgData name:@"Filedata" fileName:imgName mimeType:@"image/jpeg"];
        }
    };
    [uploadReq startWithSuccess:^(id response) {
        NSLog(@"%@", response);
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    [[FKEventTarget requestUserEvent] startWithSuccess:^(id response) {
        NSArray *arr = [FKEventModel mapArray:response];
        [arr enumerateObjectsUsingBlock:^(FKEventModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"%@", obj.created_at);
        }];
    } failure:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
